<?php

namespace CtyunOss\struct;

use CtyunOss\cors\ArrayObject;

/**
 * Class ListBucketResult
 * @package CtyunOss\struct
 *
 * @author  Violet_Ice紫冰 <violetice@aliyun.com>
 * @create  2020-06-26 21:35:57
 */
class ListBucketResult extends ArrayObject
{
    /** @var string 容器名称 */
    public $Name;

    /** @var string 前缀 */
    public $Prefix;

    /** @var mixed 起始位置 */
    public $Marker;

    /** @var string 起始位置 */
    public $NextMarker;

    /** @var int 查询条数限制 */
    public $MaxKeys;

    // TODO: 不是很明白
    /** @var bool 是否所有结果 */
    public $IsTruncated;

    /** @var FileObject[] 文件对象集合 */
    public $Contents;

    public function __construct(array $data = [])
    {
        parent::__construct($data);

        $this->MaxKeys     = (int)$data['MaxKeys'];
        $this->IsTruncated = $data['IsTruncated'] === 'false' ? false : $data['IsTruncated'] === 'true';

        $contents = $data['Contents'] ?? [];
        if (!empty($contents['ETag'])) {
            $this->Contents = [new FileObject($contents)];
        }
        else {
            $this->Contents = [];

            foreach ($contents as $content) {
                array_push($this->Contents, new FileObject($content));
            }
        }
    }
}